<?php

require FCPATH.'/application/controllers/backend/backend.php';

class News_category extends Backend{

	public function __construct(){
		parent::__construct();
		$this->load->model('news_category_model','model');
	}

	public function index(){
		$this->load_js('backend_news_category');
		$this->rend('news_category');
	}

	public function records(){
		$page = intval($this->input->get('page'));
		$page_size = intval($this->input->get('pageSize'));
		$page = $page?$page:1;
		$page_size = $page_size?$page_size:10;
		$offset = $page_size * ($page - 1);

		$conditions = array();
		$id = intval($this->input->get('id'));
		if(!empty($id)){
			$conditions[] = array('where','id',$id);
		}

		$name_zh = trim($this->input->get('name_zh'));
		if(!empty($name_zh)){
			$conditions[] = array('like','name_zh',$name_zh);
		}

		$name_fr = trim($this->input->get('name_fr'));
		if(!empty($name_fr)){
			$conditions[] = array('like','name_fr',$name_fr);
		}

		$name_en = trim($this->input->get('name_en'));
		if(!empty($name_en)){
			$conditions[] = array('like','name_en',$name_en);
		}

		$order = intval($this->input->get('order'));
		if(!empty($order)){
			$conditions[] = array('where','order',$order);
		}

		$start = $this->input->get('start');
		if(!empty($start)){
			$conditions[] = array('where','created_at',$start);
		}

		$end = $this->input->get('end');
		if(!empty($end)){
			$conditions[] = array('where','created_at',$end);
		}

		$order_fields = array('id','name_zh','name_fr','name_en','order','created_at');
		$order_field = intval($this->input->get('order_field'));
		$order_type = intval($this->input->get('order_type'));
		$orders = array();
		if ($order_field >= 0 && $order_field < count($order_fields)) {
			$orders = array(
				array($order_fields[$order_field], $order_type == 1 ? 'asc' : 'desc')
			);
		}
		$this->json_back('XT001','',array(
			'total' => $this->model->total($conditions),
			'records' => $this->model->records(array(),$conditions,$orders,$page_size,$offset)
		));
	}

	public function insert(){
		$data = array(
			'name_zh' => trim($this->input->post('name_zh')),
			'name_fr' => trim($this->input->post('name_fr')),
			'name_en' => trim($this->input->post('name_en')),
			'order' => $this->model->max_order() + 1
 		);
		if(($id = $this->model->insert($data)) > 0){
			$this->json_back('XT001','',array('id' => $id));
		}
		$this->json_back('XT002','数据库或SQL语句有误。');
	}

	public function update(){
		$id = intval($this->input->post('id'));
		$data = array(
			'name_zh' => trim($this->input->post('name_zh')),
			'name_fr' => trim($this->input->post('name_fr')),
			'name_en' => trim($this->input->post('name_en'))
 		);
		if($this->model->update($id,$data)){
			$this->json_back('XT001','',array('id' => $id));
		}
		$this->json_back('XT002','数据库或SQL语句有误。');
	}

	public function delete(){
		$id = intval($this->input->get('id'));
		if($this->model->delete($id)){
			$this->json_back('XT001','删除成功。');
		}
		$this->json_back('XT002','数据库或SQL语句有误。');
	}

}
